
local guac      = require 'guac'
local window    = require 'guac.window'
local screen    = require 'guac.screen'
local mouse     = require 'guac.mouse'
local dialog    = require 'guac.dialog'
local keyboard  = require 'guac.keyboard'
local scene     = require 'guac.scene'
local music     = require 'guac.music'
local sound     = require 'guac.sound'
local text      = require 'guac.text'

local function userWantsToQuit()
  if dialog.confirm("Are you sure you want to quit?", "Are you sure?", true) then
    window.close()
  end
end

screen.backgroundColor(1, 0, 0)

local track = music.loadfile("mus/emotional_storm.mod")
track:volume(0.1)
--track:play()

local jump = sound.loadfile("sfx/jump.ogg")

local font = text.loadfont("fnt/slkscr.ttf", 64)

local pointer
local boit

guac.launch({

  onbegin = function()
    pointer = scene.sprite({
      parent = scene.root;
      image = font:render("@"); -- screen.loadImage( 'gfx/pointer.png' );
    })
    
    boit = scene.sprite({
      parent = scene.root;
      image = screen.loadImage( 'gfx/boit.png' );
      x = 100, y = 100;
    })
  end;

  -- The vertical and horizontal resolution of the game-screen - when it is
  -- actually being played, this may not necessarily correspond to actual size in pixels.
  screen_details = {
    width = 320;
    height = 200;
  };
  
  -- This is the context that the game-screen is shown in:
  -- * Is it in a window, or full-screen?
  -- * If it's a window, what is the title bar text?
  --   What happens when you click the close button?
  window_details = {
    title = "Platformer";
    onclose = userWantsToQuit;
    fullscreen = false;
  };
  
  fps = 60;
  
  onupdate = function()
    local mouseX, mouseY = mouse.position()
    window.title((mouseX or '-') .. ',' .. (mouseY or '-'))
    if mouseX then
      pointer:show()
      pointer:position(mouseX, mouseY, 0)
    else
      pointer:hide()
    end
    if keyboard.isKeyDown(keyboard.LEFT) then
      boit.x = boit.x - 1
      boit.xflip = true
    end
    if keyboard.isKeyDown(keyboard.RIGHT) then
      boit.x = boit.x + 1
      boit.xflip = false
    end
    for i, k in keyboard.eachstruck() do
      if (k == keyboard.SPACE) then
        jump:play()
      end
      if (k == keyboard.ESC) then
        userWantsToQuit()
      end
    end
  end;
  
})
